package com.campus.server.service;

import java.util.List;

import com.campus.common.vo.BankAccount;
import com.campus.common.vo.BankTransaction;
import com.campus.server.dao.BankDao;

/**
 * BankService: 银行业务逻辑层，负责扣款/充值等操作
 */
public class BankService {
    private BankDao dao = new BankDao();

    // 支付（消费）
    public boolean pay(String userId, double amount, String password) {
        BankAccount account = dao.findAccountById(userId);
        if (account == null || !account.getPassword().equals(password)) {
            return false; // 密码错误
        }
        boolean ok = dao.deductBalance(userId, amount);
        if (ok) dao.insertTransaction(userId, "pay", amount);
        return ok;
    }


    // 查询余额
    public double queryBalance(String userId) {
        return dao.getBalance(userId);
    }

    // 充值
    public boolean recharge(String userId, double amount, String password) {
    	BankAccount account = dao.findAccountById(userId);
        if (account == null || !account.getPassword().equals(password)) {
            return false; // 密码错误
        }
        boolean ok = dao.recharge(userId, amount);
        if (ok) dao.insertTransaction(userId, "recharge", amount);
        return ok;
    }

    // 开户
    public boolean openAccount(String userId, double initBalance, String password) {
    	boolean ok = dao.createAccount(userId, initBalance, password);
        if (ok) dao.insertTransaction(userId, "open", initBalance);
        return ok;
    }
    
    // 销户
    public boolean deleteAccount(String userId, String password) {
    	boolean ok = dao.deleteAccount(userId, password);
        if (ok) dao.insertTransaction(userId, "delete", 0);
        return ok;
    }
    
    // 查询流水
    public List<BankTransaction> queryTransactions(String userId) {
        return dao.getTransactions(userId);
    }
}

